#!/bin/bash
SCRIPT_BASE_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "${SCRIPT_BASE_DIR}/common/fs_utils.sh"

kernel=tmp/deploy/images/qemux86/bzImage-qemux86.bin
hda=tmp/deploy/images/qemux86/core-image-apd-devel-radar-qemux86.ext4
mac_addr="52:55:00:d1:55:55" 
ip_addr="192.168.7.2"
gw_addr="192.169.7.1"
qemu_cmd="qemu-system-i386"
tap_name="tap0"

function usage() {
    echo "Usage: $( basename "$0" ) [-k <kernel> -f <file_system>]"
    echo "      用${qemu_cmd}运行网址为${ip_addr}的apd demo(radar)镜像"
    echo "      -k <kernel> 指定kernel镜像,默认为build/${kernel}"
    echo "      -f <file_system> 指定文件系统镜像,默认为build/${hda}"
    echo "      -h 输出本信息"
    exit 1
}

while getopts ":k:f:" arg
do
    case "${arg}" in
        k)
            kernel="${OPTARG}"
            ;;
        f)
            hda="${OPTARG}"
            ;;
        h|*)
            usage
            ;;
    esac
done

file_exists_or_die ${kernel}
file_exists_or_die ${hda}

qemu-system-i386 \
    -name radar \
    -nographic \
    -no-reboot \
    -m 256 \
    -cpu qemu32 \
    -kernel "${kernel}" \
    -hda  "${hda}" \
    -device e1000,netdev=network0,mac=${mac_addr} \
    -netdev tap,id=network0,ifname=${tap_name},script=no,downscript=no \
    --append "console=ttyS0 root=/dev/hda rw mem=256M ip=${ip_addr}::${gw_addr}:255.255.255.0 oprofile.timer=1 rootfstype=ext4" 
